package ru.spb.osll.myfit.common.filter;


import java.util.ArrayList;
import java.util.List;


/**
 * User: Alexander Opeykin
 * Date: 4/9/12
 */
public class FilterSerializer {
  public String serialize(List<PageResultFilter> filters) {
    StringBuilder result = new StringBuilder();
    for (PageResultFilter filter : filters) {
      result.append(filter.serialize());
      result.append("|");
    }
    return result.toString();
  }

  public List<PageResultFilter> deserialize(String string) {
    List<PageResultFilter> filters = new ArrayList<PageResultFilter>();

    if (string == null) {
      return filters;
    }

    for (String filterString : string.split("[|]")) {
      String [] params = filterString.split("=");

      if (!filterString.contains("=") || params[0].equals("")) {
        continue; // illegal filter
      }

      MatchFilter filter = new MatchFilter(params[0]);
      if (params.length == 2) {
        filter.setValue(params[1]);
      }
      filters.add(filter);
    }
    return filters;
  }
}
